package com.kc.demo.kcapidemo.utils;


import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


public class AESUtil {

    public static final String VIPARA = "1269571569321021";
    public static final String bm = "utf-8";


    /**
     * 字节数组转化为大写16进制字符串
     *
     * @param b
     * @return
     */
    private static String byte2HexStr(byte[] b) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < b.length; i++) {
            String s = Integer.toHexString(b[i] & 0xFF);
            if (s.length() == 1) {
                sb.append("0");
            }

            sb.append(s.toUpperCase());
        }

        return sb.toString();
    }

    /**
     * 16进制字符串转字节数组
     *
     * @param s
     * @return
     */
    private static byte[] str2ByteArray(String s) {
        int byteArrayLength = s.length() / 2;
        byte[] b = new byte[byteArrayLength];
        for (int i = 0; i < byteArrayLength; i++) {
            byte b0 = (byte) Integer.valueOf(s.substring(i * 2, i * 2 + 2), 16)
                    .intValue();
            b[i] = b0;
        }

        return b;
    }


    /**
     * AES 加密
     *
     * @param content  明文
     * @param password 生成秘钥的关键字
     * @return
     * @throws UnsupportedEncodingException
     * @throws BadPaddingException
     * @throws IllegalBlockSizeException
     */

    public static String aesEncrypt(String content, String password) {
        if (null == content) {
            return content;
        }
        IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes());
        SecretKeySpec key = new SecretKeySpec(changePassword(password).getBytes(), "AES");
        Cipher cipher = null;
        byte[] encryptedData = null;
        try {
            cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

            cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
            encryptedData = cipher.doFinal(content.getBytes(bm));
        } catch (Exception e) {
            e.printStackTrace();
        }
        //return Base64.encode(encryptedData);
        return byte2HexStr(encryptedData);
    }

    /**
     * AES 解密
     *
     * @param content  密文
     * @param password 生成秘钥的关键字
     * @return
     * @throws NoSuchPaddingException
     * @throws NoSuchAlgorithmException
     */

    public static String aesDecrypt(String content, String password) throws Exception {
        //byte[] byteMi = Base64.decode(content);
        byte[] byteMi = str2ByteArray(content);
        IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes());
        SecretKeySpec key = new SecretKeySpec(changePassword(password).getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
        byte[] decryptedData = cipher.doFinal(byteMi);
        return new String(decryptedData, "utf-8");
    }

    private static String changePassword(String password) {
        if (password.length() == 16) {
            return password;
        }
        if (password.length() < 16) {
            StringBuffer buffer = new StringBuffer(password);
            for (int i = 0; i < 16 - password.length(); i++) {
                buffer.append("0");
            }
            return buffer.toString();
        }
        return password.substring(0, 16);
    }

    public static void main(String[] args) {
        try {
            System.out.println(aesEncrypt("admin", "fa2a7aa6-68e5-48a1-9c58-7eadd094936c"));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
